package com.xinghai.web.controller.dingtalk;

import com.xinghai.common.annotation.Anonymous;
import com.xinghai.common.annotation.Log;
import com.xinghai.common.core.controller.BaseController;
import com.xinghai.common.core.domain.AjaxResult;
import com.xinghai.common.core.page.TableDataInfo;
import com.xinghai.common.enums.BusinessType;
import com.xinghai.common.exception.ServiceException;
import com.xinghai.common.utils.poi.ExcelUtil;
import com.xinghai.system.domain.XhUseVehicleInfo;
import com.xinghai.system.domain.dto.DeviceDetailDTO;
import com.xinghai.system.domain.dto.XhUseVehicleInfoDto;
import com.xinghai.system.domain.vo.XhStaticsReportQueryVo;
import com.xinghai.system.domain.vo.XhVehicleInfoQueryVo;
import com.xinghai.system.service.DingdingOaService;
import com.xinghai.system.service.IXhUseVehicleInfoService;
import com.xinghai.web.controller.dingtalk.vo.XhUseVehicleInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 用车信息Controller
 *
 * @author xinghai
 * @date 2024-07-17
 */
@RestController
@RequestMapping("/useVehicle/info")
public class XhUseVehicleInfoController extends BaseController {
    @Autowired
    private IXhUseVehicleInfoService xhUseVehicleInfoService;
    @Autowired
    private DingdingOaService dingdingOaService;


    /**
     * 查询用车信息列表
     */
    @GetMapping("/list")
    @Anonymous
    public TableDataInfo list(XhVehicleInfoQueryVo xhVehicleInfoQueryVo) {
        startPage();
        List<XhUseVehicleInfoDto> list = xhUseVehicleInfoService.list(xhVehicleInfoQueryVo);
        return getDataTable(list);
    }

    /**
     * 导出用车信息列表
     */
    @Log(title = "用车信息", businessType = BusinessType.EXPORT)
    @PostMapping("/list/export")
    public void export(HttpServletResponse response, XhVehicleInfoQueryVo xhVehicleInfoQueryVo) {
        List<XhUseVehicleInfoDto> list = xhUseVehicleInfoService.list(xhVehicleInfoQueryVo);
        ExcelUtil<XhUseVehicleInfoDto> util = new ExcelUtil<XhUseVehicleInfoDto>(XhUseVehicleInfoDto.class);
        util.exportExcel(response, list, "用车信息");
    }

    /**
     * 获取用车信息详细信息
     */
    @GetMapping("/getInfo")
    public AjaxResult getInfo(Long id) {
        return success(xhUseVehicleInfoService.getInfo(id));
    }

    /**
     * 获取用车信息详情
     */
    @GetMapping("/getDetails")
    public AjaxResult getDetails(Long id) {
        return success(xhUseVehicleInfoService.getDetails(id));
    }

    /**
     * 新增用车信息
     */
//    @PreAuthorize("@ss.hasPermi('system:info:add')")
//    @Log(title = "用车信息", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    @Anonymous
    public AjaxResult add(@RequestBody XhUseVehicleInfoVo XhUseVehicleInfoVo) {
        XhUseVehicleInfo xhUseVehicleInfo = new XhUseVehicleInfo();
        BeanUtils.copyProperties(XhUseVehicleInfoVo, xhUseVehicleInfo);
        //插入新增数据
        int row = xhUseVehicleInfoService.insert(xhUseVehicleInfo);
        boolean b = row > 0;
        //调用钉钉流程创建接口
        if (b) {
            try {
                dingdingOaService.createFlow(xhUseVehicleInfo.getId());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return toAjax(b);
    }

    /**
     * 修改用车信息
     */
    @Log(title = "用车信息", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody XhUseVehicleInfo xhUseVehicleInfo) {
        return toAjax(xhUseVehicleInfoService.update(xhUseVehicleInfo));
    }

    /**
     * 删除用车信息
     */
    @Log(title = "用车信息", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    public AjaxResult delete(Long id) {
        return toAjax(xhUseVehicleInfoService.delete(id));
    }


    /**
     * 批量删除用车信息
     */
    @Log(title = "用车信息", businessType = BusinessType.DELETE)
    @PostMapping("/deleteBatch")
    public AjaxResult deleteBatch(Long[] ids) {
        return toAjax(xhUseVehicleInfoService.deleteBatch(ids));
    }

    /**
     * 导入用车信息模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<XhUseVehicleInfo> util = new ExcelUtil<XhUseVehicleInfo>(XhUseVehicleInfo.class);
        util.importTemplateExcel(response, "用车信息数据模板");
    }


    /**
     * 导入用车信息
     */
    @Log(title = "用车信息管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) {
        ExcelUtil<XhUseVehicleInfo> util = new ExcelUtil<XhUseVehicleInfo>(XhUseVehicleInfo.class);
        List<XhUseVehicleInfo> xhUseVehicleInfoList = null;
        try {
            xhUseVehicleInfoList = util.importExcel(file.getInputStream());
        } catch (IOException e) {
            throw new ServiceException("导入数据失败");
        }
        return success(xhUseVehicleInfoService.importData(xhUseVehicleInfoList));
    }

    /**
     * 物流管理接单页面，获取用车信息
     */
    @GetMapping("/order/vehicle/info")
    public AjaxResult getOrderVehicleInfo(Long id)
    {
        try {
            return success(xhUseVehicleInfoService.getOrderVehicleInfo(id));
        }catch (Exception e){
            return error(e.getMessage());
        }
    }

}
